<template>
	<view class="wrap">
		<js-lang v-if='title' :title="title"></js-lang>
		<view class="top">
			<view style="display: flex;">
				<view style="width: 180rpx;padding-top: 20rpx;">抬头类型</view>
				<view style="display: flex;justify-content: space-between;flex: 1;">
					<view @click="ctype(2)" :class="form.ctype == 2 ? 'active': ''"
						style="border: 1px solid #000;padding: 16rpx 80rpx ;">单位</view>
					<view @click="ctype(1)" :class="form.ctype == 1 ? 'active': ''"
						style="border: 1px solid #000;padding: 16rpx 80rpx ;">个人</view>
				</view>
			</view>
			<view v-if="form.ctype == 2">
				<view class="item">
					<view class="left">名称</view>
					<input v-model="form.cname" placeholder="单位名称 (必填)" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">税号</view>
					<input  v-model="form.dutyNo" placeholder="纳税人识别号 (必填)" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">联系方式</view>
					<input v-model="form.contactInfo" placeholder="邮箱/手机任选一项填写" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">设为默认</view>
					<view
						style="width: 100%;height: 100%;text-align: right;display: flex;align-items: center;justify-content: space-between;">
						<view></view>
						<u-switch v-model="form.bdef"></u-switch>
					</view>
				</view>
			</view>
			<view v-if="form.ctype == 1">
				<view class="item">
					<view class="left">名称</view>
					<input v-model="form.cname" placeholder="真实姓名 (必填)" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">联系方式</view>
					<input v-model="form.contactInfo" placeholder="邮箱/手机任选一项填写" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">设为默认</view>
					<view
						style="width: 100%;height: 100%;text-align: right;display: flex;align-items: center;justify-content: space-between;">
						<view></view>
						<u-switch v-model="form.bdef"></u-switch>
					</view>
				</view>
			</view>

		</view>
		<view v-if="form.ctype == 2">
			<view style="padding: 10px;font-size: 24rpx;color: #aaa;">
				以下信息为非必填
			</view>
			<view class="top">
				<view class="item">
					<view class="left">开户银行</view>
					<input v-model="form.bankName" placeholder="开户银行名称" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">银行账号</view>
					<input v-model="form.bankCode" placeholder="开户银行账号" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">单位号码</view>
					<input v-model="form.dwNo" placeholder="单位号码" type="text" placeholder-class="line" />
				</view>
				<view class="item">
					<view class="left">单位地址</view>
					<input v-model="form.dwAddr" placeholder="单位地址" type="text" placeholder-class="line" />
				</view>
			</view>
		</view>

		<view style="padding: 20px;">
			<button class="login-btn" @click="save" type="primary">保存</button>
			<button v-if='title' style="margin-top: 10px;" class="login-btn" @click="ttdelete" type="warn">删除</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				form: {
					ctype: 2,
					bdef: 1
				},
			};
		},
		onLoad(params) {
			this.title = params.item ? '编辑发票抬头' : '';
			if (params.item) {
				this.form = JSON.parse(params.item)
				this.form.bdef = this.form.bdef == 1 ? true : false
			}
		},
		methods: {

			save() {
				if (!this.form.cname) {
					this.$u.toast('名称不能为空！');
					return
				}
				if (!this.form.contactInfo) {
					this.$u.toast('联系方式不能为空！');
					return
				}

				if (!this.form.dutyNo && this.form.ctype == 2) {
					this.$u.toast('税号不能为空！');
					return
				}

				this.form.cusCode = this.vuex_basCus.code
				this.form.bdef = this.form.bdef ? 1 : 0
				this.form.id = this.form.id ? this.form.id : ''
				this.$u.api.shop.ttsave(this.form).then(res => {
					if (res.result == 'false') {
						uni.showToast({
							title: res.message,
							icon: 'none',
						});
					} else {
						uni.$emit('edit', true);
						uni.navigateBack({
							delta: 1,
						})
						setTimeout(() => {
							uni.showToast({
								title: res.message,
								icon: 'none',
							});

						}, 500)

					}

				})
			},
			ttdelete() {
				this.$u.api.shop.ttdelete({
					id: this.form.id
				}).then(res => {
					if (res.result == 'false') {
						uni.showToast({
							title: res.message,
							icon: 'none',
						});
					} else {
						uni.$emit('edit', true);
						uni.navigateBack({
							delta: 1,
						})
						setTimeout(() => {
							uni.showToast({
								title: res.message,
								icon: 'none',
							});
						}, 500)
					}
				})
			},
			confirm(e) {
				this.form.picker = e.province.label + " " + e.city.label + " " + e.area.label
			},
			ctype(ctype) {
				this.form = {
					ctype,
					id: this.form.id,
					bdef: this.form.bdef,
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .line {
		color: $u-light-color;
		font-size: 28rpx;
	}

	.active {
		border: 1px red solid !important;
		color: red !important;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx $u-border-color;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 26rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx $u-border-color;

				.left {
					width: 180rpx;
				}

				input {
					text-align: left;
					width: 100%;
				}
			}

			.address {
				padding: 20rpx 0;

				textarea {
					// width: 100%;
					height: 100rpx;
					// background-color: #f7f7f7;
					line-height: 60rpx;
					// margin: 40rpx auto;
					// padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					// background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			margin-top: 20rpx;
			padding: 40rpx;
			// padding-right: 0;
			background-color: #ffffff;
			font-size: 28rpx;

			.tag {
				display: flex;

				.left {
					width: 160rpx;
				}
				
				.right {
					display: flex;
					flex-wrap: wrap;

					.tags {
						width: 140rpx;
						padding: 16rpx 8rpx;
						border: solid 2rpx $u-border-color;
						text-align: center;
						border-radius: 50rpx;
						margin: 0 10rpx 20rpx;
						display: flex;
						font-size: 28rpx;
						align-items: center;
						justify-content: center;
						color: $u-content-color;
						line-height: 1;
					}

					.active {
						color: #2979FF !important;
						border: solid 2rpx #2979FF !important;
					}

					.plus {
						//padding: 10rpx 0;
					}
				}
			}

			.default {
				margin-top: 50rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx $u-border-color;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
				}

				.right {}
			}
		}

		.login-btn {
			border-radius: 20px;
		}
	}
</style>